home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
SGI Developer Toolbox 6.1
/
SGI Developer Toolbox 6.1 - Disc 4.iso
/
src
/
haeberli
/
fonttools
/
rawtohex.c
< prev
next >
Wrap
C/C++ Source or Header
|
1994-08-01
|
4KB
|
214 lines
/*
* Copyright 1991, 1992, 1993, 1994, Silicon Graphics, Inc.
* All Rights Reserved.
*
* This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
* the contents of this file may not be disclosed to third parties, copied or
* duplicated in any form, in whole or in part, without the prior written
* permission of Silicon Graphics, Inc.
*
* RESTRICTED RIGHTS LEGEND:
* Use, duplication or disclosure by the Government is subject to restrictions
* as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
* and Computer Software clause at DFARS 252.227-7013, and/or in similar or
* successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
* rights reserved under the Copyright Laws of the United States.
*/
/*
* rawtohex -
* decode an encrypted type1 font.
*
* Paul Haeberli - 1990
*/
#include "stdio.h"
#include "ctype.h"
unsigned char *charfiledata();
FILE *inf, *outf;
int macfont;
main(argc,argv)
int argc;
char **argv;
{
int d;
char str[256];
int i, temp;
int nbytes, maxpos, patlen;
unsigned char *filedata;
int beginpos, endpos, count;
int col;
if(argc<3) {
fprintf(stderr,"usage: rawtohex in.raw out.hex\n");
exit(1);
}
inf = fopen(argv[1],"r");
if(!inf) {
fprintf(stderr,"rawtohex: can't open input file\n");
exit(1);
}
outf = fopen(argv[2],"w");
macfont = 0;
nbytes = sizeoffile(inf);
printf("nbytes is %d\n",nbytes);
filedata = charfiledata(inf);
patlen = strlen("currentfile eexec\r");
maxpos = nbytes-patlen;
for(i=0; i<nbytes; i++) {
if(strncmp(filedata+i,"currentfile eexec\r",patlen) == 0) {
beginpos = i+patlen;
printf("char at begin is %d\n",filedata[beginpos]);
break;
}
}
if(i == nbytes) {
fprintf(stderr,"rawtohex: eexec string not found\n");
exit(1);
}
for(i=0; i<beginpos; i++) {
if(filedata[i] == '\r')
filedata[i] = '\n';
}
fwrite(filedata,1,beginpos,outf);
i = nbytes;
count = 0;
while(i--) {
if(filedata[i] == '0') {
count++;
if(count == 512)
break;
} else if(!isspace(filedata[i])) {
count = 0;
}
}
if(count == 512)
fprintf(stderr,"rawtohex: found 512 zeros at %d\n",i);
else {
fprintf(stderr,"rawtohex: can't find 512 zeros!!\n");
exit(1);
}
endpos = i;
col = 0;
for(i=beginpos; i<endpos; i++) {
fprintf(outf,"%02x",filedata[i]);
col++;
if(col == 32) {
col = 0;
fprintf(outf,"\n");
}
}
if(col != 0)
fprintf(outf,"\n");
for(i=endpos; i<nbytes; i++) {
if(filedata[i] == '\r')
filedata[i] = '\n';
}
fwrite(filedata+endpos,1,nbytes-endpos,outf);
fclose(outf);
exit(0);
}
mygets(str,inf)
char *str;
FILE *inf;
{
char c;
while(1) {
c = fgetc(inf);
if((c == '\r') || (c == '\n')) {
if(c == '\r')
macfont = 1;
else
macfont = 0;
*str = 0;
return 1;
}
*str++ = c;
}
}
unsigned short int r;
unsigned short int c1 = 52845;
unsigned short int c2 = 22719;
unsigned char decrypt(cipher)
unsigned char cipher;
{
unsigned char plain;
plain = (cipher^(r>>8));
r = (cipher+r)*c1 + c2;
return plain;
}
nexthexchar(f)
FILE *f;
{
unsigned char c[1];
int n;
while(1) {
fread(c,1,1,f);
if(c[0] != '\n')
return c[0];
}
}
fromhex(c)
int c;
{
if(c>='0' && c<='9')
return c-'0';
if(c>='a' && c<='f')
return 10+c-'a';
if(c>='A' && c<='F')
return 10+c-'A';
fclose(outf);
exit(1);
}
gethexbyte(f)
FILE *f;
{
int low, hi;
hi = fromhex(nexthexchar(f));
low = fromhex(nexthexchar(f));
return ((hi<<4)+low);
}
nextbyte(f)
FILE *f;
{
if(macfont)
return fgetc(f);
else
return gethexbyte(f);
}
tohex(f)
FILE *f;
{
int i, c, d;
int pos;
pos = 0;
while(!feof(f)) {
d = nextbyte(f);
fprintf(outf,"%02x",d);
pos++;
if(pos == 32) {
fprintf(outf,"\n");
pos = 0;
}
}
fprintf(stderr,"end of file\n");
fprintf(outf,"\n");
}